﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Web.Caching;
using System.Data;
using Microsoft.Data.Odbc;
using FaithCMSControls;
using System.IO;

namespace FaithCMS
{
    public partial class FaithCMS : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //Grab the user status and convert it to a bool value
            bool _loggedIn = Convert.ToBoolean(SessionManager.UserStatus);

            //If there isn't a user logged in
            if (!_loggedIn)
            {
                //Go back to the login page
                Response.Redirect(@"~/AdminLoginPage.aspx");
            }
            else
            {
                //Create a literal control to hold the Html Controls that are needed
                LiteralControl userPanelControls = new LiteralControl("<h1 class=\"userWelcome\">Welcome to FaithCMS " + "<a href=\"#nogo\">" + SessionManager.UserID + "</a>" + "</h1>");

                //Add the literal control containing the Html Controls that are needed
                userPanel.Controls.AddAt(0, userPanelControls);

                logout.Visible = true;
                logout.Click += new EventHandler(logout_Click);
            }

            //Get the navigation bar links
            GetLinks();
        }

        /// <summary>
        /// Pull the navigation links from the database
        /// </summary>
        private void GetLinks()
        {
            //Store the links
            DataSet linkData = DatabaseManipulation.CallLinkProc();

            //If the links aren't already cached
            if (Cache["links"] == null)
            {
                //For each record returned
                foreach (DataRow r in linkData.Tables["links"].Rows)
                {
                    //Separate the links based on category
                    switch (r["category"].ToString())
                    {
                        case "uploads":
                            navLinks1.InnerHtml += "<a class=\"navLink\" id=\"" + r["link_name"] + "\" href=\"" + r["link_address"] + "\">" + r["link_name"] + "</a><br/>";
                            break;
                        case "settings":
                            navLinks2.InnerHtml += "<a class=\"navLink\" id=\"" + r["link_name"] + "\" href=\"" + r["link_address"] + "\">" + r["link_name"] + "</a><br/>";
                            break;
                        default:
                            break;
                    }
                }

                //Cache the ODBCDataAdapter (for 20 Minutes)
                Cache.Insert("links", linkData, null, DateTime.Now.AddMinutes(20), TimeSpan.Zero);
            }
            else
            {
                //Retrieve the records from the cache
                linkData = (DataSet)Cache["links"];

                //Foreach row in the table
                foreach (DataRow row in linkData.Tables["links"].Rows)
                {
                    switch (row["category"].ToString())
                    {
                        case "uploads":
                            navLinks1.InnerHtml += "<a class=\"navLink\" id=\"" + row["link_name"] + "\" href=\"" + row["link_address"] + "\">" + row["link_name"] + "</a><br/>";
                            break;
                        case "settings":
                            navLinks2.InnerHtml += "<a class=\"navLink\" id=\"" + row["link_name"] + "\" href=\"" + row["link_address"] + "\">" + row["link_name"] + "</a><br/>";
                            break;
                        default:
                            break;
                    }
                }
            }
        }

        void logout_Click(object sender, EventArgs e)
        {
            //Abandon the session
            Session.Abandon();

            //Set the user status in the SessionManager to "LoggedOut"
            SessionManager.UserStatus = SessionManager.Status.LoggedOut;

            if (Request.Cookies["FAITHCMS_LOGIN"] != null)
            {
                //Set the cookie's expiration date to the past so that it will get removed
                Request.Cookies["FAITHCMS_LOGIN"].Expires = DateTime.Now.AddDays(-10);
                
                //Add the cookie to the response
                Response.Cookies.Add(Request.Cookies["FAITHCMS_LOGIN"]);
            }
            
            //Redirect to the logon page
            Response.Redirect(@"~/AdminLoginPage.aspx");
        }
    }
}